package net.jxta.myjxta.plugins.share;

import net.jxta.myjxta.MyJXTA;
import net.jxta.myjxta.plugin.IPluginNotificationHandler;
import net.jxta.myjxta.plugin.Plugin;
import net.jxta.myjxta.plugin.PluginBase;
import net.jxta.myjxta.share.IShareManager;
import net.jxta.myjxta.share.IShareManagerProvider;
import net.jxta.myjxta.util.Group;
import net.jxta.myjxta.util.Resources;

import java.util.ArrayList;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;

public final class CmsFileSharingPlugin extends PluginBase implements Plugin, IPluginNotificationHandler, IShareManagerProvider {

    private static final Logger LOG = Logger.getLogger(CmsFileSharingPlugin.class.getName());
    private static final ResourceBundle STRINGS = Resources.getStrings();
//    HTTPServer m_httpServer=null;


    private final ArrayList<Group> m_joinedGroups = new ArrayList<Group>();


    public CmsFileSharingPlugin() {
        LOG.log(java.util.logging.Level.FINE,
                "CmsFileSharingPlugin Constructor called");
    }


    public void start() {
        super.start();

        //we want a simple test menu entry on the peer nodes
        //m_container.registerPopupProvider(this);

        //start can be called multiple times.. maybe we already have some joined groups... start listeners for em
        for (Group group : m_joinedGroups) {
            startListenerForGroup(group);
        }
    }

    public void stop() {
        //m_container.removePopupProvider(this);
        for (Group group : m_joinedGroups) {
            stopListenerForGroup(group);
        }
        super.stop();
    }

    public void destroy() {
        if (m_running) {
            stop();
        }
        super.destroy();
    }

    public String getName() {
        return "CMS FileSharing Plugin";
    }

    // END OF PLUGIN API

    // HELPER METHODS

    private void startListenerForGroup(Group p_group) {
        if (p_group.isVisible()) {
            m_container.getMyJxta().setStatus(STRINGS.getString("status.share.register"));

            try {
                ShareManager.getShareManager(m_container.getMyJxta(), p_group, true);
            } catch (Throwable t) {
                // Catch exception so that Main Initializer does not fail.
                if (net.jxta.logging.Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
                    LOG.log(Level.SEVERE, "Caught unexpected Exception", t);
                }
            }
        }


    }

    private void stopListenerForGroup(Group p_group) {
        m_container.getMyJxta().setStatus(STRINGS.getString("status.share.resign") + ": "
                + p_group.getName());

        // stop the share manger
        ShareManager shareManager = ShareManager.getShareManager(m_container.getMyJxta(), p_group,
                false);
        if (shareManager != null) {
            shareManager.unshareAll();
            shareManager.stop();
        }

    }

    public IPluginNotificationHandler getPluginNotificationHander() {
        return this;  //for now we will implement this interface ourselve, it it will grow so we cant stay here...
    }

    public void groupJoined(Group p_group) {
        if (!p_group.isVisible())
            return;

        m_joinedGroups.add(p_group);

        if (isRunning()) {
            startListenerForGroup(p_group);
        }
    }

    public void groupResigned(Group p_group) {
        if (!p_group.isVisible())
            return;

        m_joinedGroups.remove(p_group);
        if (isRunning()) {
            stopListenerForGroup(p_group);
        }
    }

    //this may be fired multiple times, for example if we change from rdv to peer node
    public void groupStateChanged(Group p_group) {
        if (!p_group.isVisible())
            return;

        if (!isRunning()) { // if we are not active we are not interested in connection events
            return;
        }


        if (p_group.isConnected()) {
            //we are connected
            if (p_group.isOwnPeerRdv()) {
                //we are a Rendeszvous
            } else {
                //we are a simple peer
            }
        } else {
            //we lost the connection to the group (so we are not a rdv and we dont have a rdv right now
        }
    }

    public IShareManager getShareManager(MyJXTA myJXTA, Group group, boolean p_create) {
        if (!m_running) {
            start();
        }
        return ShareManager.getShareManager(myJXTA, group, p_create);

    }
}
